import Vue from 'vue'
import Vuex from 'vuex'
import Router from "@/router";
import utils from "@/utils";

import { logout } from "@/api/users";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo:{},
    menu:[]
  },
  mutations: {
    login(state, data) {
      state.userInfo = data
    },
    logout(state, data) {
      state.userInfo = data
    },
    getmenu(state, data) {
      // console.log(data);

      state.menu = data
    },
  },
  actions: {
    LOGIN(context,payload){
      utils.setlocalStorage("userInfo", payload);
      context.commit('login', payload)

      Router.push({
        name: "Home",
      });
    },
    async LOGOUT(context) {
      await logout()
      utils.removelocalStorage("userInfo");
      context.commit('logout', {})

      setTimeout(() => {
        Router.replace({
          name: 'Login'
        })
      }, 0);

      // utils.removelocalStorage("userInfo");
      // utils.removelocalStorage("accessToken");
      // context.commit('logout')
    },
    // 获取菜单
    GETMENU(context,payload){
      context.commit('getmenu', payload)
    }
  },
  modules: {
  }
})
